package com.ngc.mall.controller;

import com.ngc.mall.common.configure.LocalUser;
import com.ngc.mall.common.configure.response.ResponseResult;
import com.ngc.mall.core.enumeration.CouponStatus;
import com.ngc.mall.domain.CouponDomain;
import com.ngc.mall.param.CouponParam;
import com.ngc.mall.service.ICouponService;
import com.ngc.mall.vo.UserCouponVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/coupon")
public class CouponController {
    @Resource
    private ICouponService iCouponService;

    @PostMapping("/collect")
    @ApiOperation("领取优惠券")
    public ResponseResult<Boolean> collectOneCoupon(@RequestBody CouponParam couponParam){
        Long uid= LocalUser.getUser().getId();
        Boolean result=iCouponService.collectOneCoupon(uid,couponParam.getId(),true);
        return ResponseResult.success(result);
    }
    @GetMapping("/by/category/{categoryId}")
    @ApiOperation("根据商品类目获取优惠券")
    public ResponseResult<List<CouponDomain>> getByCategory(@PathVariable Long categoryId){
        LocalDateTime now=LocalDateTime.now();
        return ResponseResult.success(iCouponService.getByCategory(categoryId,now));
    }

    @GetMapping("/my/available/with_category")
    @ApiOperation("获取我可用的优惠券（含商品分类")
    public ResponseResult<List<UserCouponVO>>  getMyAvaliableCouponsWithCategory(){
        Long uid=LocalUser.getUser().getId();
        LocalDateTime now=LocalDateTime.now();
        return ResponseResult.success(iCouponService.getMyAvaliableCouponWithCategory(uid,now));
    }

    @GetMapping("/my/by/status/{status}")
    @ApiOperation("获取我各状态的优惠券")
    public ResponseResult<List<UserCouponVO>> getMyExpired(@PathVariable Integer status){
        Long uid=LocalUser.getUser().getId();
        LocalDateTime now =LocalDateTime.now();
        List<UserCouponVO> couponList=new ArrayList<>();
        System.out.println(CouponStatus.toType(status));
        switch (CouponStatus.toType(status)){
            case AVAILABLE:
               couponList=iCouponService.findMyAvailable(uid,now);
               break;
            case USED:
                couponList=iCouponService.findMyUsed(uid,now);
                break;
            case EXPIRED:
                couponList=iCouponService.findMyExpired(uid,now);
                break;
            default:
                System.out.println("错误状态");
                break;
        }
        return ResponseResult.success(couponList);
    }

    @GetMapping("/whole_store")
    @ApiOperation("获取全场券")
    public ResponseResult<List<CouponDomain>> getMyUsed(){
        LocalDateTime now=LocalDateTime.now();
        return ResponseResult.success(iCouponService.findWholeStore(now));
    }
}
